package com.taiyuan.tongcheng.view.multichecklistview;

import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.taiyuan.tongcheng.ui.ExpressActivity;
import com.taiyuan.tongcheng.ui.MovingActivity;
import com.tytc56.user.R;

public class MovingServiceAdapter extends BaseAdapter {

	private final LinkedList<Boolean> selected = new LinkedList<Boolean>();

	private Context context;
	private LayoutInflater inflater;
	private List<MovingService> listDate;
	private ListView listView;
	private int[] defaultImg=new int[]{R.drawable.icon_tools_default,R.drawable.icon_trucktype_1_default,R.drawable.icon_return_default,R.drawable.icon_collection_default};
	private int[] checkedImg=new int[]{R.drawable.icon_tools_active,R.drawable.icon_trucktype_1_active,R.drawable.icon_return_active,R.drawable.icon_collection_active};

	public MovingServiceAdapter(Context context, List<MovingService> list,
			ListView listview) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.listDate = list;
		this.listView = listview;
		for (int i = 0; i < list.size(); i++) {
			selected.add(false);
		}
	}

	public LinkedList<Boolean> getSelect() {
		return selected;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.list_item_service, null);

			holder = new ViewHolder();
			holder.layout = (CheckableLinearLayout) convertView
					.findViewById(R.id.ll_moving_service);
			holder.textView = (CheckableTextView) convertView
					.findViewById(R.id.tv_moving_service);
			holder.imageView=(CheckableImageView)convertView.findViewById(R.id.img_moving_service);;
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final MovingService itemInfo = listDate.get(position);
		final ListView mylistView = ((ListView) ((ExpressActivity) context)
				.findViewById(R.id.lv_service));
		holder.textView.setText(itemInfo.getSetviceContent());
		holder.imageView.setDafaultImgID(defaultImg[position]);
		holder.imageView.setCheckImgID(checkedImg[position]);
		if(selected.get(position)){
			
		}else{
			holder.imageView.setBackgroundResource(defaultImg[position]);
		}
		holder.layout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				((CheckableLinearLayout) v).toggle();
				CheckableLinearLayout xx = (CheckableLinearLayout) v;
				xx.setChecked(!selected.get(position));
				mylistView.setItemChecked(position,
						((CheckableLinearLayout) v).isChecked());
				selected.set(position, !selected.get(position));
				context.sendBroadcast(new Intent("service_freash_ui"));
				notifyDataSetChanged();
			}
		});
		return convertView;
	}

	@Override
	public int getCount() {
		return listDate.size();
	}

	@Override
	public Object getItem(int position) {
		return listDate.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public class ViewHolder {
		public CheckableLinearLayout layout;
		public CheckableTextView textView;
		public CheckableImageView  imageView;
	}
}
